package com.example.su_shi_tan_mine.child

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.su_shi_tan_bas_lib.adapter.JssBaseViewHolder
import com.example.su_shi_tan_bas_lib.base.LazyFragment
import com.example.su_shi_tan_bas_lib.base.SimpleListFragment
import com.example.su_shi_tan_bas_lib.bean.CommonBean
import com.example.su_shi_tan_bas_lib.networking.RestClient
import com.example.su_shi_tan_core.arouter.MineARouter
import com.example.su_shi_tan_core.extentions.startNavigation
import com.example.su_shi_tan_mine.R
import com.example.su_shi_tan_mine.arouter.SubPageARouter
import com.example.su_shi_tan_mine.child.bean.MessagePageBean
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

@Route(path = MineARouter.MESSAGE_PAGE)
class MessagePageFragment : SimpleListFragment<MessagePageBean>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration=false
        isEnableLoadMore=false
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        addHeader(R.layout.comment_header_layout)
        val mToolbar: Toolbar =mHeader.findViewById(R.id.mToolbar)
        mToolbar.title="公告"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
    }

    override fun getItemLayout(): Int {
        return R.layout.message_page_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: MessagePageBean?) {
        helper?.setText(R.id.title,item?.title)
            ?.setText(R.id.time,item?.add_time)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(MessagePageBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        RestClient.builder()
            .url(R.string.hot_list)
            .success {
                parseDate(it)
            }.failure {
                onFailed()
            }.error { _, _ ->
                onFailed()
            }
            .build()
            .get()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val arrays:List<MessagePageBean> = JSON.parseArray(data,MessagePageBean::class.java)
        loadListDate(arrays)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        super.onItemClick(adapter, view, position)
        val item = mAdapter.getItem(position)
        ARouter.getInstance()
            .build(SubPageARouter.MessagePageDetail)
            .withString(SubPageARouter.MessagePageDetailParam,"${item?.id}")
            .startNavigation(this)
    }


}